package com.spzx.user.controller;

import com.github.pagehelper.PageHelper;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.user.api.domain.UpdateUserLogin;
import com.spzx.user.api.domain.UserInfo;
import com.spzx.product.api.domain.ProductSkuVo;
import com.spzx.user.domain.UserInfoVo;
import com.spzx.user.service.IUserBrowseHistoryService;
import com.spzx.user.service.IUserCollectService;
import com.spzx.user.service.IUserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-11-18
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController extends BaseController {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserCollectService userCollectService;
    @Autowired
    private IUserBrowseHistoryService userBrowseHistoryService;

    @InnerAuth
    @PostMapping("/register")
    public R<Boolean> register(@RequestBody UserInfo userInfo) {
        userInfoService.register(userInfo);
        return R.ok();
    }

    @Operation(summary = "根据用户名获取用户信息")
    @InnerAuth
    @GetMapping("/info/{username}")
    public R<UserInfo> getUserInfo(@PathVariable("username") String username) {
        UserInfo userInfo = userInfoService.selectUserByUserName(username);
        return R.ok(userInfo);
    }

    @Operation(summary = "更新用户登录信息")
    @InnerAuth
    @PutMapping("/updateUserLogin")
    public R<Boolean> updateUserLogin(@RequestBody UpdateUserLogin updateUserLogin) {
        return R.ok(userInfoService.updateUserLogin(updateUserLogin));
    }

    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("/getLoginUserInfo")
    public AjaxResult getLoginUserInfo(HttpServletRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        UserInfo userInfo = userInfoService.getById(userId);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);
        return success(userInfoVo);
    }
    @Operation(summary = "添加商品收藏")
    @GetMapping("/collect/{skuId}")
    public AjaxResult doCollect(
            @Parameter(description = "商品skuId")
            @PathVariable Long skuId
    ){
        userCollectService.doCollect(skuId);
        return success();
    }
    @Operation(summary = "取消商品收藏")
    @GetMapping("/cancelCollect/{skuId}")
    public AjaxResult cancelCollect(
            @Parameter(description = "商品skuId")
            @PathVariable Long skuId
    ){
        userCollectService.cancelCollect(skuId);
        return success();
    }
    @Operation(summary = "判断当前商品是否收藏")
    @GetMapping("/isCollect/{skuId}")
    public AjaxResult isCollect(@Parameter(description = "商品skuId")
                                   @PathVariable Long skuId){
        userBrowseHistoryService.saveUserBrowseHistory(skuId);
        return success(userCollectService.isCollect(skuId));
    }
    @Operation(summary = "获取收藏页列表")
    @GetMapping("/userCollectList/{pageNum}/{pageSize}")
    public TableDataInfo getUserCollectList(
            @Parameter(description = "当前页码")
            @PathVariable Integer pageNum,
            @Parameter(description = "每页记录数")
            @PathVariable Integer pageSize
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<ProductSkuVo> list = userInfoService.getUserCollectList();
        return getDataTable(list);
    }
    @Operation(summary = "获取浏览历史记录列表")
    @GetMapping("/userBrowseHistoryList/{pageNum}/{pageSize}")
    public TableDataInfo getUserBrowseHistoryList(
            @Parameter(description = "当前页码")
            @PathVariable Integer pageNum,
            @Parameter(description = "每页记录数")
            @PathVariable Integer pageSize
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<ProductSkuVo> list = userInfoService.getUserBrowseHistoryList();
        return getDataTable(list);
    }

}
